package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.Channel;
import cn.nubia.admin.application.bean.Soft;
import cn.nubia.admin.application.bean.SoftComment;
import cn.nubia.admin.application.bean.SoftCommentVO;
import cn.nubia.admin.application.query.SoftCommentQuery;
import cn.nubia.admin.application.query.SoftQuery;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 软件评论服务.
 */
@Service
public class SoftCommentService extends BaseServiceImpl<SoftComment> {


    @Autowired
    private SoftService softService;
    @Autowired
    SystemLogService systemLogService;

    /**
     * 获取列表.
     *
     * @param softCommentQuery
     * @return
     */
    public Paged<SoftCommentVO> findPagedList(SoftCommentQuery softCommentQuery) {


        Query<SoftComment> query = Query.build(SoftComment.class);
        if (softCommentQuery.getSoftName() != null && !"".equals(softCommentQuery.getSoftName())) {
            SoftQuery softQuery = new SoftQuery();
            softQuery.setSoftName(softCommentQuery.getSoftName());
            List<Soft> list = softService.findSoftList(softQuery);
            List<Integer> softIds = new ArrayList<Integer>();
            for (int i = 0; i < list.size(); i++) {
                softIds.add(list.get(i).getId());
            }
            query.addIn("softId", softIds);
        }
        query.setPaged(softCommentQuery.getPageNo(), softCommentQuery.getPageSize());
        Paged<SoftComment> paged = findPagedByQuery(query);

        List<SoftComment> softComments = paged.getListData();
        List<SoftCommentVO> softCommentVos = new ArrayList<SoftCommentVO>();
        for (int i = 0; i < softComments.size(); i++) {
            SoftCommentVO softCommentVo = new SoftCommentVO();
            BeanUtils.copyProperties(softComments.get(i), softCommentVo);
            softCommentVos.add(softCommentVo);
            Soft soft = softService.get(softComments.get(i).getSoftId());
            if(soft != null){
                softCommentVo.setSoftName(soft.getSoftName());
            }
        }
        Paged<SoftCommentVO> retPaged = new Paged<SoftCommentVO>();
        retPaged.setListData(softCommentVos);
        retPaged.setTotalCount(paged.getTotalCount());
        retPaged.setPageNo(paged.getPageNo());
        retPaged.setPageSize(paged.getPageSize());


        return retPaged;
    }

    public void changeState(Integer id, Integer state) {
        SoftComment softComment = get(id);
        softComment.setState(state);
        update(softComment);
        if(state == SoftComment.STATE_DEL){
            systemLogService.log(SystemLog.ModuleTypeEnum.SOFTCOMMENTLIST, SystemLog.OperateTypeEnum.DEL, id);
        }else if(state == SoftComment.STATE_HIDDEN){
            systemLogService.log(SystemLog.ModuleTypeEnum.SOFTCOMMENTLIST, SystemLog.OperateTypeEnum.HIDDEN, id);
        }
    }
}
